package com.core.zyyz.pcmc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.core.zyyz.pcmc.domain.PcmcRole;
import com.core.zyyz.pcmc.domain.PcmcRoleUser;
import com.core.zyyz.pcmc.domain.PcmcUser;
import com.core.zyyz.pcmc.service.PcmcRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.common.utils.poi.ExcelUtil;
import com.core.common.core.page.TableDataInfo;

/**
 * 角色Controller
 *
 * @author core
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/pcmc/role")
public class PcmcRoleController extends BaseController {

    @Autowired
    private PcmcRoleService pcmcRoleService;

    /**
     * 查询角色列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(PcmcRole pcmcRole){
        startPage();
        List<PcmcRole> list = pcmcRoleService.selectPcmcRoleList(pcmcRole);
        return getDataTable(list);
    }

    /**
     * 导出角色列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:export')")
    @Log(title = "角色", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcmcRole pcmcRole){
        List<PcmcRole> list = pcmcRoleService.selectPcmcRoleList(pcmcRole);
        ExcelUtil<PcmcRole> util = new ExcelUtil<PcmcRole>(PcmcRole.class);
        util.exportExcel(response, list, "角色数据");
    }

    /**
     * 获取角色详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping(value = "/{roleCode}")
    public AjaxResult getInfo(@PathVariable("roleCode") String roleCode){
        return success(pcmcRoleService.selectPcmcRoleByRoleCode(roleCode));
    }

    /**
     * 新增角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色", businessType = BusinessType.INSERT)
    @PostMapping("/insertRole")
    public AjaxResult add(@RequestBody PcmcRole pcmcRole) throws Exception {
        return toAjax(pcmcRoleService.insertPcmcRole(pcmcRole));
    }

    /**
     * 修改角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色", businessType = BusinessType.UPDATE)
    @PostMapping("/updateRole")
    public AjaxResult edit(@RequestBody PcmcRole pcmcRole){
        return toAjax(pcmcRoleService.updatePcmcRole(pcmcRole));
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色", businessType = BusinessType.DELETE)
    @PostMapping("/{roleCodes}")
    public AjaxResult remove(@PathVariable String[] roleCodes){
        return toAjax(pcmcRoleService.deletePcmcRoleByRoleCodes(roleCodes));
    }

    /**
     * 查询角色未授权用户
     */
    @GetMapping({"/queryRoleUnGrantUserList"})
    public TableDataInfo queryRoleUnGrantUserList() {
//        startPage();
        List<PcmcUser> list = this.pcmcRoleService.queryRoleUnGrantUserList();
        return getDataTable(list);
    }

    /**
     * 查询角色已授权用户
     */
    @PostMapping({"/queryRoleGrantUserList"})
    public TableDataInfo queryRoleGrantUserList(@RequestBody PcmcUser param) {
//        startPage();
        List<PcmcUser> list = this.pcmcRoleService.queryRoleGrantUserList(param);
          return getDataTable(list);
    }

    /**
     * 角色授权用户
     */
    @PreAuthorize("@ss.hasPermi('pcmc:user:edit')")
    @Log(title = "角色授权用户", businessType = BusinessType.GRANT)
    @PostMapping("/authRole")
    public AjaxResult insertAuthRole(@RequestBody PcmcRoleUser pcmcRoleUser) {
        pcmcRoleService.insertUserAuth(pcmcRoleUser);
        return success();
    }

}
